<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ZixunCategory extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('zixunCategory_m');
    }

    public function index()
    {
        $category         = $this->zixunCategory_m->select();
        $data['category'] = $category;
        $this->load->view('zixuncategory/index', $data);
    }

    public function add()
    {
        $this->load->view('zixuncategory/form');
    }

    public function edit($id = null)
    {
        $category = $this->zixunCategory_m->find($id);
        if (!$category) {
            show_message('参数错误！', site_url(X_URL . 'ZixunCategory/index'));
        }

        $data['category'] = $category;
        $this->load->view('zixuncategory/form', $data);
    }

    public function del($id)
    {
        if ($id) {
            if ($this->zixunCategory_m->delete($id)) {
                show_message('恭喜，删除成功！', site_url(X_URL . 'ZixunCategory/index'), 1);
            } else {
                show_message('参数错误！', site_url(X_URL . 'ZixunCategory/index'));
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function update()
    {
        $post                  = $this->input->post();
        $data['category_name'] = $post['category_name'];
        $data['order']         = $post['order'] ?? 1;
        $id                    = $post['id'] ?? 0;
        if ($data['category_name'] == '') {
            show_message('名称不能为空！', site_url(X_URL . 'ZixunCategory/index'));
        }
        if (!$id) {
            $this->zixunCategory_m->insert($data);
        } else {
            $this->zixunCategory_m->update(['id' => $id], $data);
        }
        show_message('恭喜，操作成功！', site_url(X_URL . 'ZixunCategory/index'), 1);
    }
}

